package com.atguigu.system.controller;




import com.atguig.system.annotation.Log;
import com.atguig.system.enums.BusinessType;
import com.atguigu.model.system.SysRole;
import com.atguigu.model.vo.AssginRoleVo;
import com.atguigu.model.vo.SysRoleQueryVo;
import com.atguigu.system.service.SysRoleService;
import com.atguigu.system.utils.R;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author lemon
 * @since 2023-01-22
 */
@Api(tags = "角色管理接口")
@RestController
@RequestMapping("/admin/system/sysRole")
//@CrossOrigin
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;

    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation(value = "根据用户获取角色数据")
    @GetMapping("/toAssign/{userId}")
    public R toAssign(@PathVariable("userId") Long userId) {
        Map<String, Object> roleMap = sysRoleService.getRolesByUserId(userId);
        return R.ok().put("data", roleMap);
    }

    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/doAssign")
    public R doAssign(@RequestBody AssginRoleVo assginRoleVo) {
        sysRoleService.doAssign(assginRoleVo);
        return R.ok();
    }


    //7 批量删除
    // 多个id值 [1,2,3]
    // json数组格式 --- java的list集合
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("批量删除")
    @DeleteMapping("batchRemove")
    public R batchRemove(@RequestBody List<Long> ids) {
        sysRoleService.removeByIds(ids);
        return R.ok();
    }


    //6 修改-最终修改
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @ApiOperation("最终修改")
    @PostMapping("update")
    public R updateRole(@RequestBody SysRole sysRole) {
        boolean isSuccess = sysRoleService.updateById(sysRole);
        if (isSuccess) {
            return R.ok();
        } else {
            return R.error();
        }
    }


    //5 修改-根据id查询
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("根据id查询")
    @PostMapping("findRoleById/{id}")
    public R findRoleById(@PathVariable Long id) {
        SysRole sysRole = sysRoleService.getById(id);
        return R.ok().put("data", sysRole);
    }


    //4 添加
    // @RequestBody 不能使用get提交方式
    // 传递json格式数据，把json格式数据封装到对象里面 {...}
    @Log(title = "角色管理", businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @ApiOperation("添加角色")
    @PostMapping("save")
    public R saveRole (@RequestBody SysRole sysRole) {
        boolean isSuccess = sysRoleService.save(sysRole);
        if (isSuccess) {
            return R.ok();
        } else {
            return R.error();
        }
    }


    //3 条件分页查询
    // page当前页  limit每页记录数
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("条件分页查询")
    @GetMapping("{page}/{limit}")
    public R findPageQueryRole(@PathVariable("page") Long page,
                               @PathVariable("limit") Long limit,
                               SysRoleQueryVo sysRoleQueryVo) {
        //创建page对象
        Page<SysRole> pageParam = new Page<>(page, limit);
        //调用service方法
        IPage<SysRole> pageModel = sysRoleService.selectPage(pageParam, sysRoleQueryVo);
        //返回
        return R.ok().put("data", pageModel);
    }


    @ApiOperation("查询全部的列表")
    @GetMapping("findAll")
    public R findAll() {
        List<SysRole> list = sysRoleService.findAll();
        return R.ok().put("data", list);
    }

    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @ApiOperation("逻辑删除角色接口")
    @DeleteMapping("/remove/{id}")
    public R removeById(@PathVariable("id") Long id) {
        sysRoleService.remove(id);
        return R.ok();
    }

}

